<template>
  <div>
    <el-input
      placeholder="请输入内容"
      v-model="searchinput"
      class="input-with-select"
    >
      <el-button slot="append" icon="el-icon-search"></el-button
    ></el-input>
    <el-button class="btn1" type="primary" @click="add">添加商品</el-button>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="250">
      </el-table-column>
      <el-table-column prop="goods_price" label="价格（元）"> </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
      <el-table-column prop="upd_time" label="创建时间"
        ><template slot-scope="scope">
          {{ getTime(scope.row.add_time) }}</template
        >
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.size"
      :page-sizes="[1, 4, 5, 10]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    >
    </el-pagination>
  </div>
</template>
<script>
import { getGoodslist } from "../../utils/api";
export default {
  data() {
    return {
      searchinput: "", //搜索input
      tableData: [], //表格数据
      page: {
        pagenum: 1, //当前页码
        pagesize: 5, //每页几条
      },
    };
  },
  methods: {
      add(){ //点击添加跳转添加页面
        this.$router.push('/goodsinfo')
      },
    handleSizeChange(val) { //每页几条
    //   console.log(`每页 ${val} 条`);
      this.page.pagesize=val
      this.rander()
    },
    handleCurrentChange(val) { //当前页码
    //   console.log(`当前页: ${val}`);
      this.page.pagenum=val
      this.rander()
    },
    getTime(val) {//时间
      return new Date(val * 1000).toLocaleString().split("/").join("-");
    },
    rander(){ //封装商品列表数据接口
      getGoodslist(this.page).then((res) => {//请求商品列表数据接口
      console.log(res);
      if(res.data.meta.status==200){
          this.$message({
              type: 'success', // success error warning
              message: res.data.meta.msg,
              duration: 2000,
          })
      }
      this.tableData = res.data.data.goods;
    });
    },
  },
  created() {
    this.rander()
  },
};
</script>
<style lang="scss" scoped>
.el-input {
  width: 300px;
}
.btn1 {
  margin-left: 20px;
}
</style>